//promise对象实现ajax封装
//1、设置服务器地址
const baseURL = 'http://127.0.0.1:3000';
//2、封装
const http = ({method,url,data})=>{
    const promise = new Promise((resolve,reject)=>{
        const xmlhttp = new XMLHttpRequest();
        xmlhttp.open(method, `${baseURL}/${url}`);
        if(method == 'GET'){
            xmlhttp.send();
        }else{
            xmlhttp.setRequestHeader("Content-Type","application/json");
            xmlhttp.send(JSON.stringify(data));
        };
        xmlhttp.onreadystatechange = function(){
            if(xmlhttp.readyState !== 4) return;
            if(xmlhttp.status == 200){
                resolve(xmlhttp.responseText)
            }else{
                reject(new Error(xmlhttp.responseText))
            }
        }
    });
    return promise;
}